package behavior;

import manager.Manager;

public class LocalBehaviorCalculator extends BehaviorCalculator
{
    /**
     * Argument: reference to manager
     * @param mgr 
     */
    protected LocalBehaviorCalculator(Manager mgr, double mutFac)
    {
        super.mgr = mgr;
        super.setMutationFactor(mutFac);
        super.behaviorList = new Behavior[0];
    }
    
    /**
     * Loops through behaviorList and for each calls getAgentView 
     * in Manager, then parses the resulting array into an array of 
     * floats and passes that into the proc function for that behavior. 
     * translates resulting int array into AgentAction and passes 
     * that into setAction in manager along with index of behavior.
     */
    public void procActions()
    {
        for (int i = 0; i < behaviorList.length; i++)
        {
            AgentSight[] sight = mgr.getAgentView(i);
            double[] toNet = new double[sight.length*3 + 3];
            for (int j = 0; j < sight.length; j++)
            {
                toNet[3*j] = sight[j].getSpecies();
                toNet[3*j+1] = sight[j].getLife();
                toNet[3*j+2] = 0;
                if (sight[j].isPregnant())
                {
                    toNet[3*j+2] = 1;
                }
            }
            int[] data = mgr.getSelfProcData(i);
            toNet[toNet.length-3] = (double)data[0];
            toNet[toNet.length-2] = (double)data[1];
            toNet[toNet.length-1] = (double)data[2];
            
            int[] action = behaviorList[i].proc(toNet);
            mgr.setAction(i, new AgentAction(action[0], action[1], action[2]));
            //AgentAction a = new AgentAction(action[0], action[1], action[2]);
            //return a;
        }
    }
    /**
     * Arguments:
     * -Index of behavior to remove
     */
    public void removeBehavior(int index)
    {
        Behavior[] newList = new Behavior[behaviorList.length-1];
        int ndx = 0;
        for (int i = 0; i < behaviorList.length; i++)
        {
            if (i != index)
            {
                newList[ndx] = behaviorList[i];
                ndx++;
            }
        }
        behaviorList = newList;
    }

}
